package mathgame.core;

import java.util.ArrayList;
import java.util.Random;

import mathgame.core.Chapter.ChapterNames;
import mathgame.filehandlerimpl.ImplFileHandler;

public class ProblemGenerator {

    public int var1, var2;
    public Chapter chapter;
    public int solution;
    public ImplFileHandler implFileHandler;
    public String problemText;
    public String problemSolution;
    public String equationFilepath;
    public String percentFilepath;
    public int equationProblems;
    public int percentProblems;
    
    private ArrayList <Chapter> activatedChapters = new ArrayList <Chapter>();

    public ProblemGenerator( ArrayList <Chapter> activatedChapters ) {
        this.activatedChapters = activatedChapters;
        implFileHandler = new ImplFileHandler();
        equationFilepath = "problem/Equation";
        percentFilepath = "problem/Percent";
        equationProblems = 4;
        percentProblems = 2;
    }

    public String generateProblem() {
        if( activatedChapters.size() > 0 )
        {
            chapter = activatedChapters.get(randomizeChapter());
            if(chapter.name == ChapterNames.EQUATION){
            	ArrayList<String> problem = implFileHandler.readProblem(equationFilepath +
            			(int)Math.floor( Math.random() * equationProblems + 1) + ".txt");
                problemSolution = problem.get(0);
                problemText = problem.get(1);
                return problemText;
            }
            else if(chapter.name == ChapterNames.PERCENT){
            	ArrayList<String> problem = implFileHandler.readProblem(percentFilepath +
            			(int)Math.floor( Math.random() * percentProblems + 1) + ".txt");
                problemSolution = problem.get(0);
                problemText = problem.get(1);
                return problemText;
            }
            else{
                randomizeVariables();
                calculateAnswer();
                return var1 + chapter.operator + var2;
            }
        }
        else{
            return "TODO EXCEPTION TODO";
        }
    }

    private void calculateAnswer() {
        switch( chapter.name ) {

            case ADDITION:
                solution = var1 + var2;
                break;

            case SUBTRACTION:
                solution = var1 - var2;
                break;

            case DIVISION:
                solution = var1 / var2;
                break;

            case MULTIPLICATION:
                solution = var1 * var2;
                break;
                
        }
    }

    private void randomizeVariables() {
        var1 = (int) Math.floor( Math.random() * chapter.maxValue + chapter.minValue );
        var2 = (int) Math.floor( Math.random() * chapter.maxValue + chapter.minValue );

        if ( chapter.name == ChapterNames.SUBTRACTION) {
            while (var1 < var2){
                var1 = (int) Math.floor( Math.random() * chapter.maxValue + 44 );
            }
        }
        
        if( chapter.name == ChapterNames.DIVISION ) {
            var2 = (int) Math.floor( Math.random() * (chapter.maxValue-1) + chapter.minValue );
            while (isPrime(var1)) {
                var1 = (int) Math.floor( Math.random() * chapter.maxValue );
            }
            while( !isDivisible( var2 ) ) {
                var2 = (int) Math.floor( Math.random() * chapter.maxValue + 2 );
            }
        }
    }
    			//makes sure we're not trying to divide a prime number
    private boolean isPrime(int dividend) {
        int j;

        if (dividend%2 == 0){
            return false;
        }
        for (j=3 ; j<=(dividend/2) ; j=j+2){
            if (dividend%j == 0){
                return false;
            }
        }
        return true;
    }
    		// makes sure variables can be divided
    private boolean isDivisible( int divisor ) {
        if ( var1 % divisor == 0 )
            return true;
        else
            return false;
    }

    public int randomizeChapter() {
        Random random = new Random();
        return random.nextInt(activatedChapters.size());
    }

}
